package com.example.home.library;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LibraryDetailActivity extends AppCompatActivity {

    private String libraryBean_id;
    private LibraryDetailsBean libraryDetailsBean;
    private ImageView locationImageView;
    private TextView nameTextView;
    private TextView addressTextView;
    private TextView introductionTextView;
    private TextView openingHoursTextView;
    private TextView statusTextView;
    private Button commentButton;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_library_detail);

        Intent intent = getIntent();
        libraryBean_id = intent.getStringExtra("LibraryBean_ID");
        ivitview();
        try {
            sendBean();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    private void sendBean() throws MalformedURLException {
        OkHttpClient okHttpClient = new OkHttpClient();
        URL url = new URL("http://124.93.196.45:10001/prod-api/api/digital-library/library/"+libraryBean_id);
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization", OKHttpTool.TOKEN)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.d("xhf","json失败");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json =response.body().string();
                Gson gson = new Gson();
                libraryDetailsBean = gson.fromJson(json, LibraryDetailsBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        gebean();
                    }
                });
            }
        });
    }

    private void gebean() {
        Glide.with(this).load(libraryDetailsBean.getData().getImgUrl()).into(locationImageView);
        nameTextView.setText(libraryDetailsBean.getData().getName());
        addressTextView.setText(libraryDetailsBean.getData().getAddress());
        introductionTextView.setText(libraryDetailsBean.getData().getDescription());
        openingHoursTextView.setText(libraryDetailsBean.getData().getBusinessHours());
        if(libraryDetailsBean.getData().getBusinessState().equals("1")){
            statusTextView.setText("营业中");
        }else if(libraryDetailsBean.getData().getBusinessState().equals("0")){
            statusTextView.setText("未营业");
        }
    }

    private void ivitview()  {
        locationImageView = findViewById(R.id.locationImageView);
        nameTextView = findViewById(R.id.nameTextView);
        addressTextView = findViewById(R.id.addressTextView);
        introductionTextView = findViewById(R.id.introductionTextView);
        openingHoursTextView = findViewById(R.id.openingHoursTextView);
        statusTextView = findViewById(R.id.statusTextView);
        commentButton = findViewById(R.id.commentButton);
        handler = new Handler();
    }


}